package cn.hyh.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ScheduledFuture;

/**
 * @auther :hyh
 * @desc :       测试类：动态的生成定时任务。
 * @date :2019/7/22
 */
@RestController
public class TestDynSchuled {

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    private ScheduledFuture   future;

    @RequestMapping("/start")
    public String startCron() {
        future = threadPoolTaskScheduler.schedule(new Runnable() {
            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                System.out.println(thread + "====>执行了任务。。。");
            }
        }, new CronTrigger("0/5 * * * * *"));
        return "start success....";
    }


    @RequestMapping("/stop")
    public  String  stop() {
        if (future != null) {
            future.cancel(true);
        }

        return "stop success.....";
    }




}
